package com.linghang.lhonline.controller;

import com.linghang.lhonline.Util.*;
import com.linghang.lhonline.VO.*;
import com.linghang.lhonline.dataobject.*;
import com.linghang.lhonline.service.*;
import com.linghang.lhonline.service.Impl.MissionServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/mission")
@Slf4j
public class MissionController {

    private MissionService missionService;
    private HomeworkService homeworkService;
    private UsersService usersService;
    private CommentService commentService;
    private ReplyService replyService;

    @Autowired
    public MissionController(MissionService missionService,
                             HomeworkService homeworkService,
                             UsersService usersService,
                             CommentService commentService,
                             ReplyService replyService) {
        this.missionService = missionService;
        this.homeworkService = homeworkService;
        this.usersService = usersService;
        this.commentService = commentService;
        this.replyService = replyService;
    }


    @PostMapping("/post")
    public ResultVO Post(@RequestParam("group") String groupName,
                         @RequestParam("title") String title,
                         @RequestParam("body") String body,
                         @RequestParam("end_time") Date endTime) {

        Mission mission = new Mission();

        mission.setTitle(title);
        mission.setBody(body);
        mission.setEndTime(endTime);
        mission.setGroupName(groupName);

        missionService.save(mission);

        return ResultVOUtil.success("发布成功");
    }

    @PostMapping("/update")
    public ResultVO update(@RequestParam("mission_id") Integer missionId,
                           @RequestParam("group") String groupName,
                           @RequestParam("title") String title,
                           @RequestParam("body") String body,
                           @RequestParam("end_time") Date endTime) {

        Mission mission = missionService.findByMissionId(missionId);

        if (mission == null) {
            return ResultVOUtil.error("没有此任务");
        }

        if (endTime.getTime() < System.currentTimeMillis()) {
            return ResultVOUtil.error("任务已截止，无法修改日期");
        }

        mission.setGroupName(groupName);
        mission.setTitle(title);
        mission.setBody(body);
        mission.setEndTime(endTime);

        missionService.save(mission);

        return ResultVOUtil.success("修改成功");
    }

    @PostMapping("/delete")
    public ResultVO delete(@RequestParam("mission_id") Integer missionId) {

        Mission mission = missionService.findByMissionId(missionId);

        if (mission == null) {
            return ResultVOUtil.error("没有此任务");
        }

        //删除该任务的所有评论
        List<Comment> commentList = commentService.findByMissionId(missionId);
        for (Comment comment : commentList) {
            List<Reply> replyList = replyService.findByCommentId(comment.getCommentId());
            for (Reply reply : replyList) {
                replyService.delete(reply);
            }
            commentService.delete(comment);
        }

        missionService.delete(missionId);

        return ResultVOUtil.success("删除成功");
    }

    @GetMapping("/status_list")
    public ResultVO statusList(@RequestParam("mission_id") Integer mission_id) {

        Mission mission = missionService.findByMissionId(mission_id);

        if (mission == null) {
            return ResultVOUtil.error("没有此任务");
        }

        List<Users> usersList = usersService.crewFindByGroupName(mission.getGroupName());
        List<Homework> homeworkList = homeworkService.findByMissionId(mission_id);
        List<StatusVO> statusVOList = new ArrayList<>();

        for (Users users : usersList) {
            StatusVO statusVO = new StatusVO();

            for (Homework homework : homeworkList) {
                if (homework.getStudentId().equals(users.getStudentId())) {
                    statusVO.setStatus(true);
                    break;
                }
            }

            BeanUtils.copyProperties(users, statusVO);
            statusVOList.add(statusVO);
        }

        return ResultVOUtil.success("查询组员完成情况", statusVOList);
    }

    @GetMapping("/mission_list")
    public ResultVO missionList(@RequestParam("group") String groupName) {

        List<Mission> missionList = missionService.findByGroupName(groupName);

        return ResultVOUtil.success("获取该组所有已发布任务", missionList);
    }

    @PostMapping("/deliver")
    public ResultVO deliver(@RequestParam("mission_id") Integer missionId,
                            @RequestParam("student_id") String studentId,
                            @RequestParam("homework") MultipartFile multipartFile) {
        Users users = usersService.findByStudentId(studentId);

        File file = ImageUtil.upload(multipartFile);

        if (users == null) {
            return ResultVOUtil.error("查无此人");
        }

        if (file == null) {
            return ResultVOUtil.error("没有选择文件");
        }

        Homework homework = new Homework();

        homework.setMissionId(missionId);
        homework.setStudentId(studentId);
        homework.setHomeworkUrl("https://152.136.102.20/" + file.getName());

        homeworkService.save(homework);

        return ResultVOUtil.success("作业上传成功");
    }

    @GetMapping("/homework_list")
    public ResultVO missionList(@RequestParam("mission_id") Integer missionId) {
        List<Homework> homeworkList = homeworkService.findByMissionId(missionId);

        if (homeworkList.size() == 0) {
            return ResultVOUtil.error("无此项任务");
        }

        List<HomeworkListVO> homeworkListVOList = new ArrayList<>();

        for (Homework homework : homeworkList) {
            HomeworkListVO homeworkListVO = new HomeworkListVO();
            Users users = usersService.findByStudentId(homework.getStudentId());
            homeworkListVO.setImageUrl(users.getImageUrl());
            homeworkListVO.setStudentId(users.getStudentId());
            homeworkListVO.setUserName(users.getUserName());
            homeworkListVO.setHomeworkUrl(homework.getHomeworkUrl());

            homeworkListVOList.add(homeworkListVO);
        }

        return ResultVOUtil.success("查看组员的作业", homeworkListVOList);
    }

    @PostMapping("/comment")
    public ResultVO comment(@RequestParam("mission_id") Integer missionId,
                            @RequestParam("comment_body") String commentBody,
                            @RequestParam("student_id") String studentId) {

        Mission mission = missionService.findByMissionId(missionId);
        if (mission == null) {
            return ResultVOUtil.error("无此任务");
        }

        Comment comment = new Comment();

        comment.setBody(commentBody);
        comment.setMissionId(missionId);
        comment.setStudentId(studentId);

        commentService.save(comment);
        return ResultVOUtil.success("评论成功");
    }

    @PostMapping("comment_update")
    public ResultVO commentUpdate(@RequestParam("comment_id") Integer commentId,
                                  @RequestParam("comment_body") String body) {

        Comment comment = commentService.findByCommentId(commentId);

        if (comment == null) {
            return ResultVOUtil.error("评论不存在");
        }

        comment.setBody(body);
        commentService.save(comment);
        return ResultVOUtil.success("修改成功");
    }

    @PostMapping("comment_delete")
    public ResultVO commentDelete(@RequestParam("comment_id") Integer commentId,
                                  @RequestParam("student_id") String studentId) {

        Users users = usersService.findByStudentId(studentId);
        Comment comment = commentService.findByCommentId(commentId);

        if (users == null) {
            return ResultVOUtil.error("查无此人");
        }

        if (comment == null) {
            return ResultVOUtil.error("没有该评论");
        }

        if (users.getPosition() == 0 && !comment.getStudentId().equals(studentId)) {
            return ResultVOUtil.error("此评论非此用户所发布，无法删除");
        } else {
            //删除子评
            List<Reply> replyList = replyService.findByCommentId(commentId);
            for (Reply reply : replyList) {
                replyService.delete(reply);
            }

            commentService.delete(comment);
            return ResultVOUtil.success("删除成功");
        }
    }

    @GetMapping("/comment_list")
    public ResultVO mainCommentList(@RequestParam("mission_id") Integer missionId) {

        List<Comment> commentList = commentService.findByMissionId(missionId);

        List<CommentVO> commentVOList = new ArrayList<>();

        CommentSortUtil commentSortUtil = new CommentSortUtil();
        commentList.sort(commentSortUtil);

        //找高赞评论
        Comment comment1 = new Comment();
        CommentVO commentVO1 = new CommentVO();
        int up = -1;
        for (Comment comment2 : commentList) {
            if (comment2.getUp() > up) {
                comment1 = comment2;
                up = comment2.getUp();
            }
        }
        BeanUtils.copyProperties(comment1, commentVO1);
        Users users1 = usersService.findByStudentId(comment1.getStudentId());
        commentVO1.setUserName(users1.getUserName());
        commentVO1.setImageUrl(users1.getImageUrl());
        commentVOList.add(commentVO1);

        for (Comment comment : commentList) {
            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment, commentVO);

            Users users = usersService.findByStudentId(comment.getStudentId());

            commentVO.setUserName(users.getUserName());
            commentVO.setImageUrl(users.getImageUrl());

            commentVOList.add(commentVO);
        }

        return ResultVOUtil.success("获取所有主评论", commentVOList);
    }

    @PostMapping("comment_up")
    public ResultVO commentUp(@RequestParam("comment_id") Integer commentId) {
        Comment comment = commentService.findByCommentId(commentId);
        comment.setUp(comment.getUp() + 1);
        return ResultVOUtil.success("点赞成功", comment.getUp());
    }

    @GetMapping("/reply_list")
    public ResultVO replyList(@RequestParam("comment_id") Integer commentId) {
        List<Reply> replyList = replyService.findByCommentId(commentId);

        List<ReplyVO> replyVOList = new ArrayList<>();

        for (Reply reply : replyList) {
            ReplyVO replyVO = new ReplyVO();
            BeanUtils.copyProperties(reply, replyVO);
            Users users = usersService.findByStudentId(reply.getStudentId());
            replyVO.setUserName(users.getUserName());
            replyVO.setImageUrl(users.getImageUrl());
            replyVOList.add(replyVO);
        }

        List<ReplyVO> replyVOS = new ArrayList<>();

        for (ReplyVO replyVO1 : replyVOList) {

            List<ReplyVO> childrenList = new ArrayList<>();

            for (ReplyVO replyVO2 : replyVOList) {

                if (replyVO2.getTargetId().equals(-1)) {
                    continue;
                }

                if (replyVO1.getReplyId().equals(replyVO2.getTargetId())) {
                    childrenList.add(replyVO2);
                }
            }
            replyVO1.setChildrenList(childrenList);

            if (replyVO1.getTargetId().equals(-1)) {
                replyVOS.add(replyVO1);
            }
        }

        return ResultVOUtil.success("获取评论回复", replyVOS);
    }


    @PostMapping("/reply")
    public ResultVO reply(@RequestParam("comment_id") Integer commentId,
                          @RequestParam("target_id") Integer targetId,
                          @RequestParam("body") String body,
                          @RequestParam("student_id") String studentId) {
        Reply reply = new Reply();
        reply.setBody(body);
        reply.setCommentId(commentId);
        reply.setStudentId(studentId);
        reply.setTargetId(targetId);

        replyService.save(reply);

        return ResultVOUtil.success("回复成功");
    }

    @PostMapping("/reply_update")
    public ResultVO replyUpdate(@RequestParam("reply_id") Integer replyId,
                                @RequestParam("reply_body") String body) {
        Reply reply = replyService.findByReplyId(replyId);
        reply.setBody(body);

        replyService.save(reply);
        return ResultVOUtil.success("修改成功");
    }

    @PostMapping("reply_up")
    public ResultVO replyUp(@RequestParam("reply_id") Integer replyId) {
        Reply reply = replyService.findByReplyId(replyId);
        reply.setUp(reply.getUp() + 1);

        replyService.save(reply);
        return ResultVOUtil.success("点赞成功", reply.getUp());
    }

    @PostMapping("reply_delete")
    public ResultVO deleteReply(@RequestParam("reply_id") Integer replyId,
                                @RequestParam("student_id") String studentId) {

        Users users = usersService.findByStudentId(studentId);
        Reply reply = replyService.findByReplyId(replyId);

        if (users == null) {
            return ResultVOUtil.error("查无此人");
        }

        if (reply == null) {
            return ResultVOUtil.error("没有该回复");
        }

        if (users.getPosition() == 0 && !reply.getStudentId().equals(studentId)) {
            return ResultVOUtil.error("此评论非此用户所发布，无法删除");
        } else {
            //删除子评
            DeleteUtil deleteUtil = new DeleteUtil(replyService);
            deleteUtil.deleteReply(reply);
            //TODO DeleteUtil 咋用
            return ResultVOUtil.success("删除成功");
        }
    }
}
